package com.shareach.dao.mongo.impl;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.mapreduce.GroupBy;
import org.springframework.data.mongodb.core.mapreduce.GroupByResults;
import org.springframework.data.mongodb.core.mapreduce.MapReduceResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import com.shareach.common.BaseQueryOrder;
import com.shareach.common.CollectionNames;
import com.shareach.dao.MongoDBBasicDao;
import com.shareach.dao.mongo.BasePlaceDao;
import com.shareach.domain.BasePlaceDomain;
import com.shareach.util.Globals;

@Component
public class BasePlaceDaoImpl extends MongoDBBasicDao<BasePlaceDomain>
		implements BasePlaceDao {

	@Override
	public Long insert(BasePlaceDomain domain) {
		Long id = increaseAndReturn(CollectionNames.BASEPLACE);
		domain.setId(id);
		if (null == domain.getStatus()) {
			domain.setStatus(Globals.RecordStatus.VALID);
		}
		if (null == domain.getCreatTime()) {
			domain.setCreatTime(new Date());
		}
		super.save(domain);
		return id;
	}

	@Override
	public <T> void delete(Query query, Class<T> clas) {
		mongoTemplate.remove(query, clas);
	}

	@Override
	public void update(Criteria criteria, Update update) {
		super.update(criteria, update, BasePlaceDomain.class);
	}

	@Override
	public List<BasePlaceDomain> findList(Criteria criteria) {
		return super.findList(criteria, BasePlaceDomain.class);
	}

	@Override
	public List<BasePlaceDomain> findList(Query query) {
		return super.findList(query, BasePlaceDomain.class);
	}

	@Override
	public BasePlaceDomain findOne(Query query) {
		return super.findOne(query, BasePlaceDomain.class);
	}

	@Override
	public List<BasePlaceDomain> getLastNumberByCondition(
			LinkedHashMap<String, String> conditionArgs) {
		Query query = new Query();

		query.sort().on("access_date", Order.DESCENDING);

		for (Map.Entry<String, String> singleArg : conditionArgs.entrySet()) {
			query.addCriteria(new Criteria(singleArg.getKey()).is(singleArg
					.getValue()));
		}
		return super.findList(query, BasePlaceDomain.class);
	}

	@Override
	public <T> MapReduceResults<T> groupMapReduce(Query query, String map,
			String reduce, Class<T> clas) {
		return mongoTemplate.mapReduce(query, CollectionNames.BASEPLACE, map,
				reduce, clas);
	}

	@Override
	public <T> GroupByResults<T> group(Criteria criteria, String key,
			String map, String reduce, Class<T> clas) {
		return mongoTemplate.group(criteria, CollectionNames.BASEPLACE, GroupBy
				.key(key).initialDocument(map).reduceFunction(reduce), clas);
	}

	@Override
	public <T> GroupByResults<T> groups(Criteria criteria, String map,
			String reduce, Class<T> clas, String... key) {
		return mongoTemplate.group(criteria, CollectionNames.BASEPLACE, GroupBy
				.key(key).initialDocument(map).reduceFunction(reduce), clas);
	}

	@Override
	public Page<BasePlaceDomain> getPageResult(Query query, BaseQueryOrder order) {
		return getPageResult(query, order, BasePlaceDomain.class);
	}
}
